/* 
 * File:   main.cpp
 * Author: WeiHao
 *
 * Created on February 7, 2014, 5:53 PM
 */
#include "BookingControl.h"
#include "MailControl.h"

using namespace std;

string userLogin();
char findRole(string);
void printMenu(char, string&, UsersControl&, PackageControl&, BookingControl&);

int main(int argc, char** argv) { /* 
    mailController m;
    cout << m.vmb[0].dim[0].mdate<<endl;
    cout << m.vmb[0].dim[1].mdate<<endl;
    cout << m.vmb[0].dim[2].mdate<<endl;
    
    return 0;
    */
    UsersControl u;
    PackageControl p;
    BookingControl b;

    int noOfAttempt = 1;

    int option;
    string loginString;
    bool quit = false;
    // userLogin() will return a valid identity after validating

    loginString = u.login();
    if (loginString == "DoNotExist") {
        cout << "Username do not exist! (Attempt number " << noOfAttempt << ")." << endl;
    }

    while ((loginString == "Locked" || loginString == "DoNotExist") && quit == false && noOfAttempt < 3) {

        // If userLogin() returns "Fail" these chunks will be executed
        // Users will be prompted to choose whether to login or quit
        cout << endl;
        cout << "1)     Login again" << endl;
        cout << "2)     Quit" << endl << endl;
        cout << "Please enter your option: ";
        cin >> option;

        if (option == 1) {
            loginString = u.login();

            if (loginString == "DoNotExist") {
                noOfAttempt++;
                cout << "Username do not exist! (Attempt number " << noOfAttempt << ")." << endl;
                //quit = true;
            }
        } else if (option == 2) {
            quit = true;
            cout << "Quitting ..." << endl;
        } else {
            cout << "Invalid Option!" << endl;
        }
    }

    if (noOfAttempt > 3) {
        cout << "3 Failed Login attempts! ";
    }

    char roleType;

    //cout << loginString << endl;

    if (loginString == "DoNotExist" || loginString == "Locked") {
        roleType = 'Z';
    }
    else {
        roleType = findRole(loginString);
    }
    //cout << roleType << endl;
    printMenu(roleType, loginString, u, p, b);

    return 0;
}

// check if we are using this the one below

char findRole(string loginString) {
    // extracting out the first letter of the loginString to get the role
    char * loginType = &loginString[0];

    // returns the extracted letter 
    return *loginType;

}

string userLogin() {
    UsersControl u;
    string loginString = u.login();
    return loginString;
}

void printMenu(char roleType, string &loginString, UsersControl &u, PackageControl &p, BookingControl &b) {
    int choice;

    switch (roleType) {
        case 'G':
        {
            do {
                cout << endl;
                cout << "1)   Create Package" << endl;
                cout << "2)   Update Package" << endl;
                cout << "3)   Delete Package" << endl;
                cout << "4)   Create New User Accounts" << endl;
                cout << "5)   View All Staff Accounts" << endl;
                cout << "6)   Edit Staff Accounts" << endl;
                cout << "7)   Delete Staff Accounts" << endl;
                cout << "8)   View Statistics" << endl;
                cout << "9)   Quit" << endl << endl;

                cout << "Please enter your choice: ";
                cin >> choice;
                cout << endl;

                switch (choice) {
                    case 1: cout << "Create Package" << endl;
                        p.CreatePackage();
                        break;
                    case 2: cout << "Update Package" << endl;
                        p.EditPackage();
                        break;
                    case 3: cout << "Delete Package" << endl;
                        p.DelPackage();
                        break;
                    case 4: cout << "Create New User Accounts" << endl << endl;
                        u.create_Users(roleType);
                        break;
                    case 5: cout << "View All Staff Accounts" << endl << endl;
                        u.view_Users(roleType);
                        break;

                    case 6: cout << "Edit Staff Accounts" << endl;
                        u.edit_Users(roleType);
                        break;

                    case 7: cout << "Delete Staff Accounts" << endl << endl;
                        u.delete_Users(roleType);
                        break;

                    case 8: cout << "View Statistics" << endl << endl;
                        b.view_Statistics(p);
                        break;

                    case 9: cout << "Quit" << endl;
                        break;

                    default:cout << "Invalid option!" << endl;
                        break;
                }
            } while (choice < 9 && choice != 9);
            break;
        }
        case 'H':
        {
            do {
                cout << endl;
                cout << "1) View Checked Bookings." << endl;
                cout << "2) Edit Booking Status to Waiting/Payment Required." << endl;
                cout << "3) Delete a Client Account." << endl;
                cout << "4) Quit." << endl;
                cout << "Please enter your choice: ";
                cin >> choice;
                cout << endl;

                switch (choice) {
                    case 1: cout << "View Checked Bookings." << endl << endl;
                        u.view_Users(roleType);
                        break;

                    case 2: cout << "Edit Booking Status to Waiting/Payment Required." << endl;
                        b.edit_Booking('H', "");
                        break;

                    case 3: cout << "Delete a Client Account." << endl;
                        u.delete_Users(roleType);
                        break;

                    case 4: cout << "Quit." << endl;
                        break;

                    default: cout << "Invalid option!" << endl;
                        break;
                }
            } while (choice < 4 && choice != 4);
            break;
        }
        case 'F':
        {
            do {
                cout << endl;
                cout << "1) View Approved Bookings" << endl;
                cout << "2) Edit Booking Status to Confirmed/Cancelled" << endl;
                cout << "3) Quit." << endl;
                cout << "Please enter your choice: ";
                cin >> choice;
                cout << endl;

                switch (choice) {
                    case 1: cout << "View Approved Bookings." << endl;
                        u.view_Users(roleType);
                        break;
                    case 2: cout << "Edit Booking Status to Confirmed/Cancelled." << endl;
                        b.edit_Booking('F', "");
                        break;
                    case 3: cout << "Quit." << endl;
                        break;
                    default: cout << "Invalid option!" << endl;
                        break;
                }
            } while (choice < 3 && choice != 3);
            break;
        }
        case 'B':
        {
            do {
                cout << endl;
                cout << "1) View All Submitted Bookings." << endl;
                cout << "2) Edit Booking Status to Checked." << endl;
                cout << "3) Create Client Account." << endl;
                cout << "4) Quit." << endl;

                cout << "Please enter your choice: ";
                cin >> choice;
                cout << endl;

                switch (choice) {
                    case 1: cout << "View All Submitted Bookings." << endl;
                        b.view_Booking('B', "");
                        break;
                    case 2: cout << "Edit Booking Status to Checked." << endl;
                        b.edit_Booking('B', "");
                        break;
                    case 3: cout << "Create Client Account" << endl;
                        u.create_Users(roleType);
                        break;
                    case 4: cout << "Quit." << endl;
                        break;
                    default: cout << "Invalid option! Back to Main Menu" << endl;
                        break;
                }
            } while (choice < 4 && choice != 4);

            break;
        }
        case 'C':
        {
            do {
                cout << endl;
                cout << "1) Make Bookings" << endl;
                cout << "2) Edit Booking Details" << endl;
                cout << "3) Cancel Booking" << endl;
                cout << "4) Check Mail" << endl;
                cout << "5) Quit" << endl;

                cout << "Please enter your choice: ";
                cin >> choice;
                cout << endl;
                mailController m;

                switch (choice) {
                    case 1: cout << "make bookings" << endl;
                        b.create_Booking(loginString, p);
                        break;
                    case 2: cout << "edit booking details" << endl;
                        b.edit_Booking('C', loginString);
                        break;
                    case 3: cout << "cancel booking" << endl;
                        b.del_Booking('C', loginString);
                        break;
                    case 4: cout << "check mail" << endl;
                        cout << loginString;
                        m.checkInbox(loginString);
                        break;
                    case 5: cout << "quit" << endl;
                        break;
                    default: cout << "Invalid option!" << endl;
                        break;
                }
            } while (choice < 5 && choice != 5);
            break;
        }

        case 'Z':
        {
            cout << "Quitting Application" << endl;
            break;
        }
        default:
        {
            cout << "Invalid Role Type!";
            break;
        }
    }

}





